package pld.car.service.impl;

import java.util.List;

import pld.car.bean.SettingsImg;
import pld.car.bean.Settings;
import pld.car.service.*;

public class SettingsServiceImpl extends BaseServiceImpl implements SettingsService {

	public SettingsServiceImpl() {
		System.out.println("SettingsServiceImpl init!");
	}

	// 获取 根据传入的item和setKey查询对应的settings
	public Settings querySettings(String item, String setKey) {
		String sql = "from Settings where item='" + item + "' and setKey='" + setKey + "' order by id asc";
		return (Settings) this.getSettingsDao().runSQLForSettings(sql).get(0);
	}

	// 根据ID查到对应的图片
	public SettingsImg findSettingsImgByID(int id) {
		return this.getSettingsDao().findImgByID(id);
	}

	// 删除settingsImg表中的没有imgID的记录
	public void deleteOrphanItems() {
		this.getSettingsDao().deleteOrphanItems();
	}


	// 修改settings，修改整个list
	public void update(List<Settings> hotlineList) {
		for (int i = 0; i < hotlineList.size(); i++) {
			Settings s = (Settings) hotlineList.get(i);
			this.getSettingsDao().update(s);
		}
	}

	// 修改settings，一条记录
	public void update(Settings s) {
		this.getSettingsDao().update(s);
	}

	// 获取热线和note的记录
	public List<Object> queryHotlineNote(int first, int number) {
		String sql = "select id, item, setKey, setValue, note from Settings where item='Hotline' or item='Note' order by id asc";
		return this.getSettingsDao().runSQL(sql, first, number);
	}

	// 获取settings表中item为options并且setKey为rowCountPerPage的纪录，即每页显示的记录数
	public String getOption(String item, String setKey) {
		String sql = "from Settings where item='" + item + "' and setKey='" + setKey + "'";
		List<Object> optionList = this.getSettingsDao().runSQL(sql);
		String setValue = ((Settings) optionList.get(0)).getSetValue();
		return setValue;
	}

	// 获取 服务热线
	public List<Settings> queryHotlineList() {
		String sql = "from Settings where item='Hotline' order by id asc";
		return this.getSettingsDao().runSQLForSettings(sql);
	}

	// 获取 旅客留言
	public Settings queryNote() {
		String sql = "from Settings where item='Note' and setKey='note1' order by id asc";
		return (Settings) this.getSettingsDao().runSQLForSettings(sql).get(0);
	}

	// 获取 系统选项
	public List<Settings> queryOptions() {
		String sql = "from Settings where item='Options' order by id asc";
		return this.getSettingsDao().runSQLForSettings(sql);
	}
}
